#!/bin/bash

echo '==========================================================='
echo '===Welcome to Big Data Analysis Auto Installation Sctipt==='
echo 'Big Data Analysis and Processing Course By ldj@cqupt.edu.cn'
echo '==========================================================='

# Install Docker With Offitial Script
if hash docker 2>/dev/null;
then
    echo "docker has been already installed"
else
    edition=`awk -F= '/^NAME/{print $2}' /etc/os-release`
    if [[ $edition =~ "CentOS Linux" ]]; then
        echo "Your system is Centos"
        echo "Installing Docker"
        yum -y install yum-utils curl
        # yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
        # yum-config-manager \
        #     --add-repo \
        #     https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
        yum-config-manager \
            --add-repo \
            http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        yum install -y docker-ce-18.03.1.ce
    elif [[ $edition =~ "Ubuntu" ]]; then
        echo "Your system is Ubuntu"
        apt install curl -y
        
        curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    fi
    
    # Copy the mirror configuration to /etc/docker
    echo 'Adding mirror acceleration'
    docker_json_dire="/etc/docker"
    if [ ! -d "${docker_json_dire}" ];
    then
        mkdir "${docker_json_dire}"
    fi
    
    cp daemon.json /etc/docker/daemon.json 

    systemctl enable docker
    systemctl start docker
fi






# Install Docker-compose
if hash docker-compose 2>/dev/null;
then
    echo "docker-compose has already installed"
else
    echo "Installing Docker-compose"
    # curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    cp docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
fi




# Unzip Hadoop-docker.zip
unzip hadoop-docker.zip
cd hadoop-docker-centos7/

# Pull Some Docker Images
docker pull mysql:5.7
docker pull centos:7
docker pull twinsen/os-jvm:centos6-openjdk8
docker pull twinsen/hadoop:2.7.2
docker pull twinsen/hive:2.1.1
docker pull twinsen/spark:2.3.0
docker pull twinsen/hbase:1.2.5
docker pull leidj/sqoop:1.0.2
docker pull twinsen/zookeeper:3.4.10
docker network create hadoop-docker
docker-compose up -d


echo 'sleeping 10s for service up'
sleep 10s
docker-compose exec hbase-master hdfs namenode -format
docker-compose exec hbase-master schematool -dbType mysql -initSchema
echo 'sleeping 10s for creating table'
sleep 10s
docker-compose exec hbase-master jar cv0f /code/spark-libs.jar -C /root/spark/jars/ .
docker-compose exec hbase-master start-dfs.sh
docker-compose exec hbase-master hadoop fs -mkdir -p /user/spark/share/lib/
docker-compose exec hbase-master hadoop fs -put /code/spark-libs.jar /user/spark/share/lib/
docker-compose exec hbase-master stop-dfs.sh
docker-compose exec hbase-master start-dfs.sh
docker-compose exec hbase-master start-yarn.sh
docker-compose exec hbase-master start-all.sh
docker-compose exec hbase-master start-hbase.sh
docker-compose exec hbase-master /bin/bash

# Check whether installation succeeded or not
# sqoop list-databases --connect jdbc:mysql://mysql:3306/ --username root --password hadoop